package processing_layer;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * This class is part of the XSLT advanced feature, it is used to retreive the user's
 * registered courses, and construct an XML which can be used by the XSLT transformer
 */
public class XSLTHandler
{
	public static StringWriter generateSessionsXML(ArrayList<CourseSession> sessions)
	throws ParserConfigurationException, TransformerException
	{
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
		
		// Root
		Document document = docBuilder.newDocument();
		Element rootElement = document.createElement("time_table");
		document.appendChild(rootElement);

		/* They're sorted by day, then starting hour, we made sure there are no conflicts */
		Collections.sort(sessions);
		
		HashMap<String, Element> courses = new HashMap<String, Element>(); 
		
		
		for (int i = 0 ; i < sessions.size() ; i++)
		{
			final CourseSession session = sessions.get(i);
			
			Element courseElm;
			
			if (courses.containsKey(session.courseName))
			{
				courseElm = courses.get(session.courseName);
			}
			else
			{
				courseElm = document.createElement("course");
				courseElm.setAttribute("course_name", session.courseName);
				courseElm.setAttribute("course_group", Integer.toString(session.groupNumber));
				rootElement.appendChild(courseElm);
				courses.put(session.courseName, courseElm);
			}
			
			Element sessionElm = document.createElement("session");
			sessionElm.setAttribute("start", Integer.toString(session.startingTime));
			sessionElm.setAttribute("end", Integer.toString(session.endingTime));
			sessionElm.setAttribute("day", Integer.toString(session.day));
			
			courseElm.appendChild(sessionElm);
		}
			
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(); 
        
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		
		DOMSource source = new DOMSource(document);
        StringWriter outputXML = new StringWriter();
        StreamResult result = new StreamResult(outputXML);
        
        transformer.transform(source, result);
		return outputXML;
	}
}
